/*   Copyright (C) 2008  Jeroen v. Schagen & Youri Lima
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections;

using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace Engine.Rendering
{
    public enum MeshType
    {
        MT_MODEL,
        MT_TERRAIN,
        MT_SKYBOX
    }

    public class MeshManager
    {
        private static MeshManager _instance;
        private ArrayList _collection;

        private MeshManager()
        {
            _collection = new ArrayList();
        }

        public bool LoadMesh(String name, String file, MeshType type)
        {
            _collection.Add(new MeshInformation(name, file, type));

            return true;
        }

        public MeshInformation GetMesh(String name)
        {
            foreach (MeshInformation nm in _collection)
            {
                if (nm.Name == name)
                {
                    return nm.Clone();
                }
            }

            return null;
        }

        public void Init(Device device)
        {
            foreach (MeshInformation nm in _collection)
            {
                try
                {
                    if (nm.File != null && nm.File != String.Empty)
                    {
                        ExtendedMaterial[] materials = null;

                        // Initiate mesh
                        nm.Mesh = Mesh.FromFile(nm.File, MeshFlags.SystemMemory, device, out materials);

                        // Initiate textures and material
                        if ((materials != null) && (materials.Length > 0))
                        {
                            nm.Textures = new Texture[materials.Length];
                            nm.Materials = new Material[materials.Length];

                            for (int i = 0; i < materials.Length; i++)
                            {
                                nm.Materials[i] = materials[i].Material3D;
                                nm.Materials[i].Ambient = nm.Materials[i].Diffuse;

                                if (materials[i].TextureFilename != null && (materials[i].TextureFilename != String.Empty))
                                    nm.Textures[i] = TextureLoader.FromFile(device, "data/texture/" + materials[i].TextureFilename);
                            }
                        }

                        nm.Loaded = true;
                    }

                    else
                    {
                        throw new Exception("No source for the meshfile specified");
                    }
                }

                catch (Exception) {}
            }
        }

        // Singleton
        public static MeshManager Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new MeshManager();
                }

                return _instance;
            }
        }
    }
}
